﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using IOP.Extension.DependencyInjection;
using IOP.ISEA;
using Silk.NET.Vulkan;

namespace VkSample1211
{
    [ModulePriority(ModulePriority.Assets)]
    [ParentModule(typeof(PipelineModule))]
    public class AssetsModule : VulkanModule, IVulkanRenderWapperModule
    {
        public VulkanRenderWapper RenderWapper { get; set; }
        [Autowired]
        private ILogger<AssetsModule> _Logger { get; set; }

        protected override async Task Load(VulkanGraphicsManager graphicsManager)
        {
            try
            {
                var basePath = AppContext.BaseDirectory;
                var lDevice = graphicsManager.VulkanDevice;

                if (Parent is not PipelineModule pipelineModule) throw new NullReferenceException("Parent Module lost");
                ISEADocument document = new();
                await document.Load(Path.Combine(AppContext.BaseDirectory, "test1211.isea"));
                var renders = await graphicsManager.CreateRenderObjectsFromISEA<RenderObject>(document);

                renders[0].GetTransform().Position = new Vector3(0, -10f, -70f);
                var commonArchetype = ContextManager.CreateArchetype("Common", new TransformComponent());
                ContextManager.CreateContexts(1, commonArchetype, pipelineModule.CommonGroup, renders[0], false);
            }
            catch (Exception e)
            {
                _Logger?.LogError(e.Message + "\r\n" + e.StackTrace);
            }
        }

        protected override Task Unload(VulkanGraphicsManager manager)
        {
            return Task.CompletedTask;
        }
    }
}
